package com.smartwebx.ha.web.admin.module.screen.report;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.dataresolver.Param;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.assign.HaDirectoryManager;
import com.smartwebx.ha.biz.assign.HaReportAreaManager;
import com.smartwebx.ha.biz.assign.HaReportUserManager;
import com.smartwebx.ha.biz.assign.HaUserManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.assign.bean.HaDirectory;
import com.smartwebx.ha.dal.assign.bean.HaReportArea;
import com.smartwebx.ha.dal.assign.bean.HaReportAreaExample;
import com.smartwebx.ha.dal.assign.bean.HaReportUser;
import com.smartwebx.ha.dal.assign.bean.HaUser;
import com.smartwebx.ha.dal.assign.dao.HaDirectoryMapper;
import com.smartwebx.ha.dal.assign.dao.HaReportAreaMapper;
import com.smartwebx.ha.dal.assign.dao.HaUserMapper;
import com.smartwebx.ha.web.admin.common.util.HaTool;

public class AjaxInsertHaReportAreaAndUser {
	@Autowired
	private HaReportAreaManager haReportAreaManager;
	@Autowired
	private HaReportUserManager haReportUserManager;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HaUserMapper haUserMapper;
	@Autowired
	private HttpServletResponse resp;
	@Autowired
	private HaReportAreaMapper haReportAreaMapper;
	@Autowired
	private HaDirectoryMapper haDirectoryMapper;
	@Autowired
	private HaDirectoryManager haDirectoryManager;
	@Autowired
	private HaUserManager haUserManager;
	public void execute(@Param("data") String datas , @Param("driectorId") String driectorIds, @Param("checked") String checked, @Param("haAreaId") String haAreaId, @Param("type") String type) throws IOException{
		PrintWriter out = resp.getWriter();
		if("1".equals(type)){
			List<HaDirectory> haDirectory = haDirectoryManager.getDirectoryData("reportId", Integer.parseInt(driectorIds), 1);
			driectorIds = HaTool.getValue(driectorIds,haDirectory);
		}
		if("false".equals(checked)){
			HaDirectory haDirectoryLists = this.haDirectoryMapper.selectByPrimaryKey(Integer.parseInt(driectorIds));
			HaReportAreaExample haReportAreaExample = new HaReportAreaExample();
			com.smartwebx.ha.dal.assign.bean.HaReportAreaExample.Criteria haReportAreacriteria = haReportAreaExample.createCriteria();
			haReportAreacriteria.andPageIdEqualTo(haDirectoryLists.getReportId());
			haReportAreacriteria.andAreaIdEqualTo(Integer.parseInt(haAreaId));
			int haReportAreaLists = this.haReportAreaMapper.deleteByExample(haReportAreaExample);
			out.print("success");
		}else if("true".equals(checked)){
			ObjectMapper mapper = new ObjectMapper();
			HaDirectory haDirectory = haDirectoryManager.select(Integer.parseInt(driectorIds));
			int pageId = haDirectory.getReportId();
			String dateJson;
			int areaId;
			if(datas.indexOf(",")==-1){
				areaId = Integer.parseInt(datas.substring(1, datas.length()));
				dateJson = "{\""+"pageId\":"+pageId+",\"areaId\":"+areaId+"}";
				HaReportArea haReportArea = mapper.readValue(dateJson,
						HaReportArea.class);
				haReportAreaManager.insert(haReportArea);
				//insertReportUser(areaId,pageId);
			}else if(datas.indexOf(",")>=0){
				String[] areaIds = datas.split(",");
				for(String areaId2 : areaIds){
					areaId = Integer.parseInt(areaId2.substring(1, areaId2.length()));
					dateJson = "{\""+"pageId\":"+pageId+",\"areaId\":"+areaId+"}";
					HaReportArea haReportArea = mapper.readValue(dateJson,
							HaReportArea.class);
					haReportAreaManager.insert(haReportArea);
					//insertReportUser(areaId,pageId);
				}
			}
			out.print("success");
		}
	}
	
	public void insertReportUser(int areaId, int pageId) throws IOException{
		ObjectMapper mapper = new ObjectMapper();
		HaArea haArea = haAreaManager.findOne(areaId);
		String code = haArea.getCode();
		
		List<HaUser> haUsers = haUserManager.getUserData("code", code);
		int userId = haUsers.get(0).getId();
		String dateJson = "{\""+"pageId\":"+pageId+",\"userId\":"+userId+"}";
		HaReportUser haReportUser = mapper.readValue(dateJson,
				HaReportUser.class);
		haReportUserManager.insert(haReportUser);
		
		if(areaId!=1){
			haArea = haAreaManager.findOne(areaId);
			int parentId = haArea.getParentId();
			insertReportUser(parentId,pageId);
		}
	}
}
